// storage.go
package controllers

import (
	//"encoding/json"
	"log"
	//"myk8s/common"
	m "myk8s/models"
	"strings"

	beego "github.com/beego/beego/v2/server/web"
	"github.com/tidwall/gjson"
)

type StorageController struct {
	beego.Controller
}

func (this *StorageController) StorageclassList() {
	clusterId := this.GetString("clusterId")
	List, err := m.StorageclassList(clusterId)
	msg := "success"
	code := 0
	count := len(List)
	if err != nil {
		log.Println(err)
		msg = err.Error()
		code = -1
	}
	this.Data["json"] = &map[string]interface{}{"code": code, "msg": msg, "count": count, "data": &List}
	this.ServeJSON()
}

func (this *StorageController) PvList() {
	clusterId := this.GetString("clusterId")
	List, err := m.PersistentVolumeList(clusterId)
	msg := "success"
	code := 0
	count := len(List)
	if err != nil {
		log.Println(err)
		msg = err.Error()
		code = -1
	}
	this.Data["json"] = &map[string]interface{}{"code": code, "msg": msg, "count": count, "data": &List}
	//this.Data["json"] = &datas
	this.ServeJSON()
}

func (this *StorageController) PvcList() {
	clusterId := this.GetString("clusterId")
	nameSpace := this.GetString("nameSpace")
	pvcName := this.GetString("pvcName")

	labels := this.GetString("labels")
	labelsKV := strings.Split(labels, ":")
	var labelsKey, labelsValue string
	if len(labelsKV) == 2 {
		labelsKey = labelsKV[0]
		labelsValue = labelsKV[1]
	}

	pvcList, err := m.PersistentVolumeClaimList(clusterId, nameSpace, pvcName, labelsKey, labelsValue)
	msg := "success"
	code := 0
	count := len(pvcList)
	if err != nil {
		log.Println(err)
		msg = err.Error()
		code = -1
	}

	this.Data["json"] = &map[string]interface{}{"code": code, "msg": msg, "count": count, "data": &pvcList}
	//this.Data["json"] = &datas
	this.ServeJSON()
}
